﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace UpdateProject
{
    public partial class FormAo : Form
    {
        public FormAo()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
        }
        #region chayexe
        public void chayexe(string exepath)
        {
            //Application.StartupPath + @"\AutoUpdate.EXE";
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = exepath;

                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi " + ex.ToString(), "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region backgroundWorker1_ProgressChanged
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (!backgroundWorker1.CancellationPending)
            {
                lblThongbao.Text = "Hoàn Thành Được " + e.ProgressPercentage + "% Tiến Trình Update Chương Trình Từ Version ";
                //lblPercent.Text = e.ProgressPercentage + "%";
                //ProgressBar1.PerformStep();
            }
        }
        #endregion

        #region backgroundWorker1_RunWorkerCompleted
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
            chayexe(Application.StartupPath + @"\TKCCLD.EXE");
        }
        #endregion

        #region backgroundWorker1_DoWork
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DoSomething();
        }
        #endregion

        #region DoSomething
        private void DoSomething()
        {
            while (progressBar1.Value < progressBar1.Maximum)
            {
                if (backgroundWorker1.CancellationPending)
                {
                    break;
                }
                //báo cáo tiến độ của tiến trình
                progressBar1.Value = progressBar1.Value + 1;
                backgroundWorker1.ReportProgress(progressBar1.Value);
                Thread.Sleep(5);
            }
            // Thread.Sleep(20000);
        }
        #endregion

        #region formload
        private void FormAo_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
        #endregion
    }
}
